<?php

namespace addons\zzyy\controller\api;

use app\admin\model\zzyy\ZzyyAgreement;
use app\common\controller\Api;
use app\common\exception\UploadException;
use app\common\library\Upload;
use think\Config;

/**
 * 公共接口
 */
class Common extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';


    public function upload()
    {
        Config::set('default_return_type', 'json');
        //必须设定cdnurl为空,否则cdnurl函数计算错误
        Config::set('upload.cdnurl', '');

        $attachment = null;
        //默认普通上传文件
        $file = $this->request->file('file');
        try {
            $upload = new Upload($file);
            $attachment = $upload->upload();
        } catch (UploadException $e) {
            $this->error($e->getMessage());
        }

        $this->success('ok', ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
    }

    public function getagreement()
    {
        $id = $this->request->get('id');
        $agreement = ZzyyAgreement::get($id);

        $this->success('ok', [
            'title' => $agreement->agreement_title,
            'content' => $agreement->agreement_content,
        ]);
    }
}
